package com.ys.nms.module.business.controller.admin.mempools;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.mempools.vo.*;
import com.ys.nms.module.business.dal.dataobject.mempools.MempoolsDO;
import com.ys.nms.module.business.service.mempools.MempoolsService;

@Tag(name = "管理后台 - 内存")
@RestController
@RequestMapping("/business/mempools")
@Validated
public class MempoolsController {

    @Resource
    private MempoolsService mempoolsService;

    @PostMapping("/create")
    @Operation(summary = "创建内存")
    @PreAuthorize("@ss.hasPermission('business:mempools:create')")
    public CommonResult<Integer> createMempools(@Valid @RequestBody MempoolsSaveReqVO createReqVO) {
        return success(mempoolsService.createMempools(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新内存")
    @PreAuthorize("@ss.hasPermission('business:mempools:update')")
    public CommonResult<Boolean> updateMempools(@Valid @RequestBody MempoolsSaveReqVO updateReqVO) {
        mempoolsService.updateMempools(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除内存")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:mempools:delete')")
    public CommonResult<Boolean> deleteMempools(@RequestParam("id") Integer id) {
        mempoolsService.deleteMempools(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除内存")
                @PreAuthorize("@ss.hasPermission('business:mempools:delete')")
    public CommonResult<Boolean> deleteMempoolsList(@RequestParam("ids") List<Integer> ids) {
        mempoolsService.deleteMempoolsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得内存")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:mempools:query')")
    public CommonResult<MempoolsRespVO> getMempools(@RequestParam("id") Integer id) {
        MempoolsDO mempools = mempoolsService.getMempools(id);
        return success(BeanUtils.toBean(mempools, MempoolsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得内存分页")
    @PreAuthorize("@ss.hasPermission('business:mempools:query')")
    public CommonResult<PageResult<MempoolsRespVO>> getMempoolsPage(@Valid MempoolsPageReqVO pageReqVO) {
        PageResult<MempoolsDO> pageResult = mempoolsService.getMempoolsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MempoolsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出内存 Excel")
    @PreAuthorize("@ss.hasPermission('business:mempools:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMempoolsExcel(@Valid MempoolsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MempoolsDO> list = mempoolsService.getMempoolsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "内存.xls", "数据", MempoolsRespVO.class,
                        BeanUtils.toBean(list, MempoolsRespVO.class));
    }

}